package com.tianyang.yukuaifu.base;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.utils.ScreenUtil;


/**
 * Description : popWindow基类
 */

public abstract class BasePopWindow extends PopupWindow {
    private Activity mContext;
    private View mView;
    private View view;

    protected BasePopWindow(Activity context) {
        mContext = context;
    }

    /**
     * 设置背景(用于向上弹的popWindow)
     *
     * @param color 透明度
     */
    public void darkenBackGround(Float color) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = color;
        mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mContext.getWindow().setAttributes(lp);
    }

    /**
     * 设置背景
     *
     */
    private void  setPullBack(Activity activity, int top) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        if (mView == null) {
            mView = LayoutInflater.from(activity).inflate(R.layout.view_parent_add_view, null);
            view = mView.findViewById(R.id.add_view);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
            if(top==0){
                top= ScreenUtil.dp2px(92);
            }
            params.setMargins(0,top,0,0);
            view.setBackgroundColor(activity.getResources().getColor(R.color.alpha_40_black));
            activity.getWindow().addContentView(mView,lp);
        }
    }

    /**
     * 显示可见性(用于向下弹的popWindow)
     */
    public void setDismissView(Activity activity, boolean isVis, int top) {
        if(view!=null){
            if(isVis){
                view.setVisibility(View.VISIBLE);
            }else{
                view.setVisibility(View.GONE);
            }
        }else {
            setPullBack(activity,top);
        }
    }
    /**
     * 显示可见性(用于向下弹的popWindow)
     */
    public void setDismissView(boolean isVis,int top) {
        if(view!=null){
            if(isVis){
                view.setVisibility(View.VISIBLE);
            }else{
                view.setVisibility(View.GONE);
            }
        }else {
            setPullBack(mContext,top);
        }
    }

    @Override
    public void dismiss() {
        if(view!=null){
            view.setVisibility(View.GONE);
        }
        view=null;
        mView=null;
        darkenBackGround(1f);
        super.dismiss();
    }
}
